<?php 
## include required files
/*******************************/
require_once '../model/moduleuser.php';
require_once '../model/admindetails.php';
/*******************************/
## Create Objects
/*******************************/
$moduleObj = new Model_ModuleUser();
$adminObj = new Model_AdminDetails();
/*******************************/
if(isset($_POST['update'])) {
		$uid = base64_decode($_POST['hidden_userid']);
		if($uid) {
			## Delete pervious selected modules permission
			$moduleObj->deleteModuleUserValueByUserId($uid);
//		echo"<pre>";	print_R($_POST['checkall']);die();
			$chk = $_POST['checkall'];
			foreach($chk as $module_id) {
				$array = array();
				$array['module_id'] = $module_id;
				$array['user_id'] = $uid;
				$array['perm_status'] = "1";
				$moduleObj->addModuleUserValue($array);
			}
		
		$_SESSION['msg']="<div class='success_msg'><span>Permissions updated successfully</span></div>";
		header('location:'.SITE_URL.'/admin/home.php?q=permissions&id='.base64_encode($uid));
		exit;
	} else {
		$_SESSION['msg']="<div class='error_msg'><span>Select admin user to set permission</span></div>";	
	}
}

$modules = $moduleObj->getAllMasterActiveModules();
## If user is selected
// Getting all admin module permissiion status
$i=0;
if(isset($_GET["id"])) {
	$user_id = base64_decode($_GET["id"]);
	$uncheckedvalue =1;
	foreach($modules as $rs) {
		$row = $moduleObj->getModulePermissionStatusByUserId($user_id, $rs['module_id']);
		$modules[$i]['perm_status'] = $row['perm_status'];
		if($modules[$i]['perm_status'] == 1)
		{
			$smarty->assign('checkedvalue', '1');				
		}

		if($modules[$i]['perm_status'] == '')
		{
			$uncheckedvalue = '0';
		}
		$i++;
	}
}
$smarty->assign('uncheckedvalue', $uncheckedvalue);				
$smarty->assign('modules', $modules);

// Getting all Active Admin user list
$userArray = $adminObj->getAllActiveAdminUsers();
$smarty->assign("userArray", $userArray);




## Assign success or error msg to smarty variable and unset session variable
if(trim($_SESSION['msg']) != '') {
	$smarty->assign('msg', $_SESSION['msg']);
	unset($_SESSION['msg']);
}
## Set active class variable for left menu  (main menu and sub menu)
$smarty->assign('activeclass', 'adminusers');
$smarty->assign('mainmenu', '4');

## Unset all the objects created which are on this page
unset($adminObj);
unset($moduleObj);

$smarty->display(TEMPLATEDIR_ADMIN. 'adminusers/permissions.tpl');
unset($smarty);
?>